#include <unistd.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>

#include "rcv_from_server.h"


//static void debug_rcv_server_data(GameCmd cmd, unsigned char score, unsigned char mvp);
static void debug_rcv_server_data(unsigned char score, unsigned char mvp);

extern int skt_fd;

/*
*@func:接收服务器数据的线程
*@desc:从套接字中，读取服务器发送过来的数据（数据结构为ServerData），并解析其中的数据（包括屏幕的显示、成绩的显示等等）
*/
void *rcv_data(void *arg)
{   
    int ret =  0;

    while(1)
    {
        ServerData sdata;
        bzero(&sdata, sizeof(sdata));
        ret = read(skt_fd, &sdata, sizeof(sdata));

        if(ret < 0)
        {
            perror("client send failed!");
            exit(-1);
        }
        else if(ret == 0)
        {

        }
        else//成功获取了ServerData，接下里进行解析数据
        {
            pc_parse_map(sdata.m_part_map);
            //debug_rcv_server_data(sdata.m_cmd, sdata.m_score, sdata.m_mvp);
            debug_rcv_server_data(sdata.m_score, sdata.m_mvp);
        }
    }

    close(skt_fd);
}


//static void debug_rcv_server_data(GameCmd cmd, unsigned char score, unsigned char mvp)
static void debug_rcv_server_data(unsigned char score, unsigned char mvp)
{
    //0. m_part_map
    //这里的工作应该由map_parse.c完成
    
    //1. cmd
    #if 0
    switch(cmd)
    {
        default:
            printf("current player's server cmd is %d\n", cmd);
    }
    #endif
    //2.score
    printf("score is %d\t", score);

    //3.mvp
    printf("mvp is %d\n", mvp);
    printf("\n\n");
}

